package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.common.vo.AdvanceSearchProviderCondition;
import com.gxa.common.vo.Provider;
import com.gxa.common.vo.TransProvider;
import com.gxa.entity.SysProvider;
import com.gxa.mapper.ProviderManagementMapper;
import com.gxa.service.ProviderManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;


@Service
public class ProviderManagementServiceImpl implements ProviderManagementService {

    @Autowired
    ProviderManagementMapper providerManagementMapper;

    int i = -1;

    /**
     * 分页查询供应商信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R findProviderList(Integer currentPage, Integer pageSize) {

        R r = new R();

        if (currentPage != null && pageSize != null) {
            PageHelper.startPage(currentPage,pageSize);
            List<Object> providerList = providerManagementMapper.findProviderList();

            PageInfo<SysProvider> pageInfo = new PageInfo(providerList);

            r.fromStatus(Status.SUCCESS);

            r.setData(pageInfo);
        }else {
            r.fromStatus(Status.BAD_QUERY_PARAM);
        }

        return r;
    }

    /**
     * 添加供应商
     * @param provider 供应商参数对象
     * @return r接收对象
     */
    @Override
    @Transactional
    public R addProvider(Provider provider) {
        R r = new R();

        if (provider != null) {
            providerManagementMapper.addProvider(provider);

            Integer id = provider.getId();
            if (id != null) {
                providerManagementMapper.addProviderExtra(provider);
            }

            r.setCode(200);
            r.setMsg("新增成功");
        }else {
            r.fromStatus(Status.BAD_REQUEST_PARAM);
        }

        return r;
    }

    /**
     * 转交供应商
     * @param transProvider
     * @return
     */
    @Override
    public boolean transProvider(TransProvider transProvider) {
        if (transProvider != null) {
            Integer[] providerIds = transProvider.getProviderIds();
            Integer userId = transProvider.getUserId();
            for(Integer providerId:providerIds){
                i = providerManagementMapper.transProvider(providerId,userId);
            }
        }

        return i>=0?true:false;
    }

    /**
     * 通过id删除供应商
     * @param id
     * @return
     */
    @Override
    public R deleteProviderById(Integer id) {
        R r = new R();
        if (id != null) {
            try {
                r.setCode(200);
                r.setMsg("修改成功");
                r.setData(providerManagementMapper.deleteProviderById(id));
            }catch (Exception e){
                e.printStackTrace();
                r.setCode(500);
                r.setMsg("服务器开小差了，请联系管理员");
            }
        }else {
            r.fromStatus(Status.BAD_QUERY_PARAM);
        }
        return r;
    }

    /**
     * 高级查找供应商
     * @param advanceSearchProviderCondition 查找条件
     * @return
     */
    @Override
    public R advanceSearchProvider(AdvanceSearchProviderCondition advanceSearchProviderCondition) {

        R<Object> r = new R<>();
        if (advanceSearchProviderCondition != null) {
//            PageHelper.startPage(Integer.valueOf(advanceSearchProviderCondition.getCurrentPage()),Integer.valueOf(advanceSearchProviderCondition.getPageSize()));
            List list = providerManagementMapper.advanceSearchProvider(advanceSearchProviderCondition);
            r.fromStatus(Status.SUCCESS);
            r.setData(list);
        }else {
            r.fromStatus(Status.SYSTEM_ERROR);
        }
        return r;
    }

    /**
     * 修改供应商
     * @param provider 供应商对象
     * @return
     */
    @Override
    @Transactional
    public R updateProvider(Provider provider) {
        R r = new R();
        if (provider != null) {
            int i = providerManagementMapper.updateProvider(provider);

            int j = providerManagementMapper.updateProviderExtra(provider);
            if (i >=0 || j>=0) {
                r.fromStatus(Status.SUCCESS);
            }else {
                r.fromStatus(Status.SYSTEM_ERROR);
            }
        }else {
            r.fromStatus(Status.BAD_REQUEST_PARAM);
        }
        return r;
    }

    @Override
    public R selectProviderByQuery(Integer currentPage,Integer pageSize,String findProviderCondition) {

        R<Object> r = new R<>();
        if (findProviderCondition != null || findProviderCondition.equals("")) {
            PageHelper.startPage(currentPage,pageSize);
            List list = providerManagementMapper.selectProviderByQuery(findProviderCondition);
            PageInfo pageInfo = new PageInfo(list);
            r.fromStatus(Status.SUCCESS);
            r.setData(pageInfo);
        }else {
            r.fromStatus(Status.BAD_REQUEST_PARAM);
        }
        return r;

    }


}


